﻿using System;
using System.Web.UI.WebControls;

namespace Salient.Web.Administration.Security.Users
{
    public partial class findUsers : SecurityPage
    {
        private const string DATA_SOURCE = "WebAdminDataSource";

        public void SetDataSource(object v)
        {
            Session[DATA_SOURCE] = v;
        }

        public void BindGrid(bool displayNoUsersCreated)
        {
            dataGrid.DataSource = Session[DATA_SOURCE];
            dataGrid.DataBind();
            if (dataGrid.Rows.Count == 0)
            {
                if (displayNoUsersCreated)
                {
                    noUsers.Visible = true;
                }
                else
                {
                    notFoundUsers.Visible = true;
                }
            }
        }

        public void IndexChanged(object sender, GridViewPageEventArgs e)
        {
            dataGrid.PageIndex = e.NewPageIndex;
            BindGrid(false);
        }

        public void Page_Load()
        {
            if (!IsPostBack)
            {
                ClearUserCollection();
            }

            noUsers.Visible = false;
            if (!IsPostBack)
            {
                PopulateRepeaterDataSource();
                alphabetRepeater.DataBind();

                Int32 total = 0;
                dataGrid.DataSource = CallWebAdminHelperMethod(true, "GetAllUsers",
                                                               new object[] {0, Int32.MaxValue, total},
                                                               new Type[]
                                                                   {
                                                                       typeof (int), typeof (int),
                                                                       Type.GetType("System.Int32&")
                                                                   });
                SetDataSource(dataGrid.DataSource);
                BindGrid(true);
            }

            dataGrid.HeaderStyle.HorizontalAlign = DirectionalityHorizontalAlign;
        }

        public void Cancel_Click(object sender, EventArgs e)
        {
            ClearUserCollection();
            ReturnToPreviousPage(sender, e);
        }

        public void SelectedChanged(object sender, EventArgs e)
        {
            var checkBox = (CheckBox) sender;
            var item = (GridViewRow) checkBox.Parent.Parent;
            var label = (Label) item.FindControl("userNameLabel");
            string userID = label.Text;
            // Add/remove the user in the collection
            UserCollection[userID] = checkBox.Checked ? (object) true : null;
        }

        public void OK_Click(object sender, EventArgs e)
        {
            ReturnToPreviousPage(sender, e);
        }

        private void PopulateRepeaterDataSource()
        {
            PopulateRepeaterDataSource(alphabetRepeater);
        }

        public void RedirectToAddUser(object sender, EventArgs e)
        {
            CurrentUser = null;
            // do not prepend ~/ to this path since it is not at the root
            Response.Redirect("adduser.aspx");
        }

        public void RetrieveLetter(object sender, RepeaterCommandEventArgs e)
        {
            RetrieveLetter(sender, e, dataGrid);
            SetDataSource(dataGrid.DataSource);
            BindGrid(false);
        }

        protected void RoleMembershipChanged(object sender, EventArgs e)
        {
            try
            {
                var box = (CheckBox) sender;

                // Array manipulation because cannot use Roles static method (need different appPath).
                string u = CurrentUser;
                string role = box.Text;

                if (box.Checked)
                {
                    CallWebAdminHelperMethod(false, "AddUsersToRoles", new object[] {new[] {u}, new[] {role}},
                                             new[] {typeof (string[]), typeof (string[])});
                }
                else
                {
                    CallWebAdminHelperMethod(false, "RemoveUsersFromRoles", new object[] {new[] {u}, new[] {role}},
                                             new[] {typeof (string[]), typeof (string[])});
                }
            }
            catch
            {
                // Ignore, e.g., user is already in role.
            }
        }

        public void SearchForUsers(object sender, EventArgs e)
        {
            SearchForUsers(sender, e, alphabetRepeater, dataGrid, dropDown1, textBox1);
            SetDataSource(dataGrid.DataSource);
            BindGrid(false);
        }
    }
}